package DD9;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

import org.junit.Test;


public class ColTest {


	public static void main(String[] args) {
		ColTest C1 = new ColTest();
//		C1.test1();
//		C1.test2();
//		C1.test3();
//		C1.test4();
		C1.test5();
	}
	public static void show(Collection Col1) {
		Iterator IT = Col1.iterator();
		while (IT.hasNext()) {
			System.out.println(IT.next());
		}
	}
	
	private void test1() { 

// Use Collection method
		Collection Col1 = new ArrayList();	
		Col1.add(new Dog(12,"AA"));
		Col1.add(new Dog(2,"bb"));
		Col1.add(new Dog(4,"cc"));
		Col1.add(new Dog(6,"cc"));
		show(Col1);
	}
	private void test2() { 
		Collection Col1 = new ArrayList();	
		Collection Col2 = new ArrayList();	
		int [] a = {12,45,87,89};
		Col2.add("123");
		Col1.add(new Dog(12,"AA"));
		Col1.add(new Dog(2,"bb"));
		Col1.add(new Dog(4,"cc"));
		Col1.add(new Dog(6,"cc"));
		Col2.addAll(Col1);
		System.out.println(Col2.size());
		System.out.println(Col2.isEmpty());
		System.out.println(Col2.contains(new Dog(2,"bb")));
//		show(Col2);
//		Col2.clear();
//		show(Col2);
	}
	@Test
	private void test3() {
		Collection Col1 = new ArrayList();	
		Col1.add(11);
		Col1.add(22);
		Col1.add(33);
		Col1.add(44);
		Object[] arr = Col1.toArray();
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}
	private void test4() {
		// use set to make Collection
		TreeSet set = new TreeSet();
		Dog d1 = new Dog(12,"AA");
		Dog d2 = new Dog(3,"BB");
		Dog d3 = new Dog(4,"CC");
		Dog d4 = new Dog(6,"CC");
		set.add(d1);
		set.add(d2);
		set.add(d3);
		set.add(d4);
		Iterator iterator = set.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
	}
	private void test5() {
		TreeSet set = new TreeSet(new Comparator() {
			@Override
			public int compare(Object arg0, Object arg1) {
				if (arg0 instanceof Dog&& arg1 instanceof Dog) {
					Dog d1 = (Dog)arg0;
					Dog d2 = (Dog)arg1;
					if (d1.age>d2.age) {
						return 1;
					}else {
						return -1;
					}
				}
				throw new RuntimeException("Wrong type!");
			}
		});
		Dog d1 = new Dog(12,"AA");
		Dog d2 = new Dog(3,"BB");
		Dog d3 = new Dog(4,"CC");
		Dog d4 = new Dog(6,"CC");
		set.add(d1);
		set.add(d2);
		set.add(d3);
		set.add(d4);
		Iterator iterator = set.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
	}
}
